/**
 依赖

 "socket.io": "^4.4.0"

 */

var M=require("ming_node");
var app=M.server();
app.set("views","./")
const server= app.listen(8888);
global.socketClientList=[];
var io = require('socket.io')(server, {
    allowEIO3: true,
    cors: {
        origin: "*", // from the screenshot you provided
        methods: ["GET", "POST"]
    }
});

global.socketIO=io;


console.log("socketController start");
//监听上线
io.on('connection', function(socket){
    //连接参数

    console.log("===============>")
    console.log(socket.request._query);
    global.socketClientList.push(socket);
    socketIO.emit("event",{
        method: "huifu",
        params:{
            data:"hello"
        }
    });
    socket.on('event', function(msg){
        console.log('event被触发:',  msg );
    });
    //移除离线的人
    socket.on('disconnect', function(){
        let n = socketClientList.indexOf(socket);
        if(n !=-1) {
            socketClientList.splice(n,1);
        }
        console.log("disconnect")
    });
});

//发布消息
app.get("event",(req,res)=>{
    socketIO.emit("event",{
        method: "huifu",
        params:{
            data:"hello"
        }
    });
    res.send("ok")
})

//在线用户
app.get("socketClientList",(req,res)=>{
    let uidList=  global.socketClientList.map(u=>u.request._query.uid)
    res.send(uidList)
})

